﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.CollectControl;

namespace University.Workshop.Presentation.CollectControl
{
    /// <summary>
    /// Interaction logic for SearchPayments.xaml
    /// </summary>
    public partial class SearchPayments : Page, ISearchPaymentView
    {
        PresenterSearchPayment pSearchWaterPurchase;
        Dictionary<string, ICollection<Model.Allotment>> manzanos;
        bool buscar;

        public SearchPayments()
        {
            pSearchWaterPurchase = new PresenterSearchPayment(this);
            manzanos = new Dictionary<string, ICollection<Model.Allotment>>();

            InitializeComponent();
            InicializarFechas();
            LlenarCbx();
        }

        public void LlenarCbx()
        {
            manzanos = pSearchWaterPurchase.LlenarCbx();

            foreach (KeyValuePair<string, ICollection<Model.Allotment>> kv in manzanos)
            {
                ManzanoCbx.Items.Add(kv.Key);
            }
        }

        public void InicializarFechas()
        {
            FinalDateDpck.Text = DateTime.Today.ToString();

            string mes = DateTime.Today.Month.ToString();
            string anho = DateTime.Today.Year.ToString();
            string fecha = "01/" + mes + "/" + anho;

            InitialDateDpck.Text = fecha;
        }

        /*private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            pSearchWaterPurchase.SearchPayments();
        }*/

        #region ISearchWaterPurchaseView Members

        public ICollection<Model.Payment> PaymentFinded
        {
            get
            {
                if (ChargeFindedDgv.ItemsSource == null)
                    new List<Model.Payment>();
                return (ICollection<Model.Payment>)ChargeFindedDgv.ItemsSource;
            }
            set
            {
                ChargeFindedDgv.ItemsSource = null;
                ChargeFindedDgv.ItemsSource = value;
            }
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        /*public void NotifySystemMessage(string p)
        {
            //MessageBox.Show(App.Current.MainWindow, MessageValue, "Atencion!!!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }*/

        private void ShowUserBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new CollectControl.ReadPayment((Model.Payment)ChargeFindedDgv.SelectedItem));
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Historial Cobros";
        }

        public bool NotifyConfirmationMessage(string MessageValue)
        {
            return MessageBox.Show(App.Current.MainWindow,
                MessageValue,
                "Atención!!!",
                MessageBoxButton.YesNo,
                MessageBoxImage.Information,
                MessageBoxResult.Yes,
                MessageBoxOptions.RightAlign) == MessageBoxResult.Yes;
        }

        public Model.Payment PaymentSelected
        {
            get { return (Model.Payment)ChargeFindedDgv.SelectedItem; }
        }

        public string Manzano
        {
            get
            {
                return ManzanoCbx.SelectedValue.ToString();
            }
        }

        public string Lote
        {
            get
            {
                return LoteCbx.SelectedValue.ToString();
            }
        }

        public string NombreSocio
        {
            set
            {
                if (value != String.Empty)
                    SocioLbl.Content = value;
                else
                    SocioLbl.Content = "-";
            }
        }

        public string CiSocio
        {
            set
            {
                if (value != String.Empty)
                    CiLbl.Content = value;
                else
                    CiLbl.Content = "-";
            }
        }

        public decimal TotalAmount
        {
            set
            {
                /*decimal monto = value;
                int entero = Convert.ToInt32(monto);
                TotalAmountLbl.Content = entero.ToString();*/

                if (value == 0)
                    TotalAmountLbl.Content = "-";
                else
                    TotalAmountLbl.Content = value;

            }
        }

        public DateTime InitialDate
        {
            get
            {
                return DateTime.Parse(InitialDateDpck.Text);
            }
            /*set
            {
                InitialDateDpck.Text = DateTime.Today.ToString();
                /*if (value.ToString() == "01/01/0001 12:00:00 a.m.")
                    InitialDateDpck.Text = DateTime.Today.ToString();
                else
                    InitialDateDpck.Text = value.ToString();
            }*/
        }

        public DateTime FinalDate
        {
            get
            {
                return DateTime.Parse(FinalDateDpck.Text);
            }
            /*set
            {
                FinalDateDpck.Text = DateTime.Today.ToString();
             
/*
                if (value.ToString() == "01/01/0001 12:00:00 a.m.")
                    FinalDateDpck.Text = DateTime.Today.ToString();
                else
                    FinalDateDpck.Text = value.ToString();
            }*/
        }

        #endregion

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        private void CiTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void CiTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsLetterOrDigit(ch) || ch.Equals('_'));
            }
        }

        private void DatesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
                e.Handled = true;
            if (e.Key == Key.Enter)
                e.Handled = true;
            if (e.Key == Key.Delete)
                e.Handled = true;
            if (e.Key == Key.Back)
                e.Handled = true;
        }

        private void DatesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = true;
        }

        private void ManzanoCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CleanSearch();

            if (!LoteCbx.IsEnabled)
                LoteCbx.IsEnabled = true;
            else
            {
                if (LoteCbx.HasItems)
                    buscar = false;
                LoteCbx.Items.Clear();
            }

            ICollection<Model.Allotment> lotes = manzanos[ManzanoCbx.SelectedItem.ToString()];

            foreach (Model.Allotment a in lotes)
                LoteCbx.Items.Add(a.AllotmentName);

            buscar = true;
        }

        private void LoteCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //bool encontro = false;
            CleanSearch();

            if (buscar)
                pSearchWaterPurchase.SearchPayments();

            /*if (encontro)
                RealizarBtn.IsEnabled = true;
            else
                RealizarBtn.IsEnabled = false;*/
        }

        private void CleanSearch()
        {
            NombreSocio = string.Empty;
            CiSocio = string.Empty;
            //DetailsTxt.Text = string.Empty;
            TotalAmountLbl.Content = "-";
            ChargeFindedDgv.ItemsSource = null;
        }

        

    }
}
